package com.huatai.verity.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.huatai.common.api.R;
import com.huatai.common.util.ObjectUtil;
import com.huatai.common.util.StringUtil;
import com.huatai.verity.entity.AuthManage;
import com.huatai.verity.service.AuthManageService;
import com.huatai.verity.vo.AuthManageParmVo;
import com.huatai.verity.vo.SignCodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * @program: ht-system
 * @description: 授权码控制器
 * @author: songxiude
 * @create: 2023-07-25 09:15
 **/
@RestController
@RequestMapping("/authmanage")
@Api(value = "授权码管理",tags = "授权码获取更新管理")
public class AuthManageController {
	@Autowired
	private AuthManageService authManageService;

	@PostMapping("/getSignCode")
	@ApiOperation(value = "获取授权码",notes = "获取授权码")
	public R<Map<String,String>> getSignCode(@Valid @RequestBody SignCodeVo signCodeVo){
		return R.data(authManageService.getSignCode(signCodeVo));
	}

	@PostMapping("/saveUpdate")
	@ApiOperation(value = "保存更新授权码",notes = "新增更新授权码，授权码一旦授出使用无法销毁")
	public R saveUpdate(@Valid @RequestBody AuthManage authManage){
		return R.status(authManageService.saveUpdate(authManage));
	}

	@PostMapping("/delete")
	@ApiOperation(value = "删除授权码",notes = "该删除仅仅删除系统信息无法销毁授权码")
	public R delete(@RequestParam("id") Long id){
		return R.status(authManageService.removeById(id));
	}

	@PostMapping("/getAuthManageList")
	@ApiOperation(value = "获取授权码列表",notes = "支持授权服务、单位模糊查询")
	@ApiOperationSupport(order = 1, includeParameters = {
		"authManageParmVo.serviceName",
		"authManageParmVo.authObject",
		"authManageParmVo.page",
		"authManageParmVo.size"
	})
	public R<IPage<Object>> getAuthManageList(@RequestBody AuthManageParmVo authManageParmVo){
		return R.data(authManageService.getAuthManageList(authManageParmVo));
	}


	@GetMapping(value = "/exportSignXml",produces = "application/xml;charset=utf-8")
	@ApiOperation(value = "导出授权签名xml文件",notes = "导出授权签名xml文件")
	public R exportSignXml(@ApiParam(value = "签名id",name = "id",required = true) @RequestParam("id") Long id,
						   HttpServletResponse response){
		Assert.isTrue(ObjectUtil.isNotEmpty(id),"id不能为空");
		authManageService.exportSignXml(id,response);
		return R.success("导出license文件成功，请到对应的路径下查看");
	}


}
